
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;


public class PasswordTool {
    private final static String[] hexDigits = {"0", "1", "2", "3", "4",  
        "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};  
    
    public static String generatePassword(String inputString){  
        return encodeByMD5(inputString);  
    }  
    
    public static String generatePasswordBase64(String inputString){  
        return encodeByMD5Base64(inputString);  
    }  
    
  public static boolean validatePassword(String password, String inputString){  
      if(password.equals(encodeByMD5(inputString))){  
          return true;  
      } else{  
          return false;  
      }  
  }  
  public static boolean validatePasswordBase64(String password, String inputString){  
      if(password.equals(encodeByMD5Base64(inputString))){  
          return true;  
      } else{  
          return false;  
      }  
  }  
  
  
  public static String encodeByMD5(String originString){  
      if (originString != null){  
          try{  
              MessageDigest md = MessageDigest.getInstance("MD5");  
              byte[] results = md.digest(originString.getBytes());  
              String resultString = byteArrayToHexString(results);  
              return resultString.toUpperCase();  
          } catch(Exception ex){  
              ex.printStackTrace();  
          }  
      }  
      return null;  
  }  
  
  public static String encodeByMD5Base64(String originString){  
      if (originString != null){
    	  BASE64Encoder baseEncoder = new BASE64Encoder();
          try{  
              MessageDigest md = MessageDigest.getInstance("MD5");  
              byte[] results = md.digest(originString.getBytes("utf-8"));  
              String resultString = baseEncoder.encode(results); 
              return resultString;  
          } catch(Exception ex){  
              ex.printStackTrace();  
          }  
      }  
      return null;  
  }  
  
  
  public static String byteArrayToHexString(byte[] b){  
        StringBuffer resultSb = new StringBuffer();  
        for (int i = 0; i < b.length; i++){  
            resultSb.append(byteToHexString(b[i]));  
        }  
        return resultSb.toString();  
    }  
      
  public static String byteToHexString(byte b){  
        int n = b;  
        if (n < 0)  
            n = 256 + n;  
        int d1 = n / 16;  
        int d2 = n % 16;  
        return hexDigits[d1] + hexDigits[d2];  
    }    
    
  public static void main(String[] args) throws NoSuchAlgorithmException, IOException {
		String password = "admin";
//		String md5Base64Password=null;
//		String md5ByteToHexPassword=null;
//		
//		MessageDigest md5 = MessageDigest.getInstance("MD5");
		BASE64Encoder baseEncoder = new BASE64Encoder();
//		md5Base64Password = baseEncoder.encode(md5.digest(password.getBytes("utf-8")));
//		md5ByteToHexPassword=byteArrayToHexString(md5.digest(password.getBytes("utf-8")));
//		
//		System.out.println(md5Base64Password);
//		System.out.println(md5ByteToHexPassword.toUpperCase());//使用中
//		System.out.println(encodeByMD5Base64(password));
		
		
//		//--base 64 encode decode
		BASE64Decoder base64Decoder=new BASE64Decoder();
		byte[] pwdByte = password.getBytes();
		byte bFix = 1;
		System.out.println(pwdByte[0]);
		pwdByte[0]=(byte) (pwdByte[0]+bFix);
		System.out.println(pwdByte[0]);
		String encodedPwd = baseEncoder.encode(pwdByte);
		System.out.println(encodedPwd);
		byte[] decodeBytes = base64Decoder.decodeBuffer(encodedPwd);
		decodeBytes[0]=(byte) (decodeBytes[0]-bFix);
		System.out.println(new String(decodeBytes));
	}
}
